﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Gateway;
using Agresso.Interface.Layer.Domain;
using System.Data.SqlClient;
using Agresso.Interface.Layer.SQL;
using System.Collections;
using Agresso.Interface.Layer.Service;

namespace Agresso.Interface.Layer.Service
{
    public class CommissionManagersLegalEntityService
    {
        DataBaseConnectivity m_dataBaseConectivity;
        public CommissionManagersLegalEntityService()
        {
            m_dataBaseConectivity = new DataBaseConnectivity();
        }
        public void Insert(CommissionManagersLegalEntity e)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(CommissionManagersLegalEntitySQL.InsertCommissionManagersLegalEntity(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@profit_centre_softmar", System.Data.SqlDbType.NVarChar).Value = e.profit_centre_softmar;
                cmd.Parameters.Add("@profit_centre_agresso", System.Data.SqlDbType.NVarChar).Value = e.profit_centre_agresso;
                cmd.Parameters.Add("@profit_centre_agresso_dim_val", System.Data.SqlDbType.NVarChar).Value = e.profit_centre_agresso_dim_val;
                cmd.Parameters.Add("@vessel_softmar", System.Data.SqlDbType.NVarChar).Value = e.vessel_softmar;
                cmd.Parameters.Add("@vessel_agresso", System.Data.SqlDbType.NVarChar).Value = e.vessel_agresso;
                cmd.Parameters.Add("@vessel_agresso_dim_val", System.Data.SqlDbType.NVarChar).Value = e.vessel_agresso_dim_val;
                cmd.Parameters.Add("@commercial_manager", System.Data.SqlDbType.NVarChar).Value = e.commercial_manager;
                cmd.Parameters.Add("@commercial_manager_dim_val", System.Data.SqlDbType.NVarChar).Value = e.commercial_manager_dim_val;
                cmd.Parameters.Add("@created_date", System.Data.SqlDbType.DateTime).Value = DateTime.Now.ToString();
                cmd.Parameters.Add("@modified_date", System.Data.SqlDbType.DateTime).Value = DateTime.Now.ToString();
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                cmd.CommandText = CommissionManagersLegalEntitySQL.GetMaxID();
                e.cmle_id = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
        public void Update(CommissionManagersLegalEntity e)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(CommissionManagersLegalEntitySQL.UpdateCommissionManagersLegalEntity(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@profit_centre_softmar", System.Data.SqlDbType.NVarChar).Value = e.profit_centre_softmar;
                cmd.Parameters.Add("@profit_centre_agresso", System.Data.SqlDbType.NVarChar).Value = e.profit_centre_agresso;
                cmd.Parameters.Add("@profit_centre_agresso_dim_val", System.Data.SqlDbType.NVarChar).Value = e.profit_centre_agresso_dim_val;
                cmd.Parameters.Add("@vessel_softmar", System.Data.SqlDbType.NVarChar).Value = e.vessel_softmar;
                cmd.Parameters.Add("@vessel_agresso", System.Data.SqlDbType.NVarChar).Value = e.vessel_agresso;
                cmd.Parameters.Add("@vessel_agresso_dim_val", System.Data.SqlDbType.NVarChar).Value = e.vessel_agresso_dim_val;
                cmd.Parameters.Add("@commercial_manager", System.Data.SqlDbType.NVarChar).Value = e.commercial_manager;
                cmd.Parameters.Add("@commercial_manager_dim_val", System.Data.SqlDbType.NVarChar).Value = e.commercial_manager_dim_val;
                cmd.Parameters.Add("@modified_date", System.Data.SqlDbType.DateTime).Value = DateTime.Now.ToString();
                cmd.Parameters.Add("@cmle_id", System.Data.SqlDbType.Int).Value = e.cmle_id;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
        public void Delete(CommissionManagersLegalEntity e)
        {
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(CommissionManagersLegalEntitySQL.DeleteCommissionManagersLegalEntity(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@cmle_id", System.Data.SqlDbType.Int).Value = e.cmle_id;
                cmd.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
        public IList SelectAll()
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(CommissionManagersLegalEntitySQL.SelectAll(), m_dataBaseConectivity.InterfaceSqlConnection);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result.Add(new CommissionManagersLegalEntity(rd));
                }
                rd.Close();
                return result;
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        public IList Find(string p)
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(CommissionManagersLegalEntitySQL.SelectAll(p), m_dataBaseConectivity.InterfaceSqlConnection);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result.Add(new CommissionManagersLegalEntity(rd));
                }
                rd.Close();
                return result;
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }

        internal CommissionManagersLegalEntity GetByProfitCentreAndVessel(string profitcentre, string vessel)
        {
            CommissionManagersLegalEntity result = null;
            try
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Open();
                SqlCommand cmd = new SqlCommand(CommissionManagersLegalEntitySQL.GetByProfitCentreAndVessel(), m_dataBaseConectivity.InterfaceSqlConnection);
                cmd.Parameters.Add("@profit_centre_softmar", System.Data.SqlDbType.NVarChar).Value = profitcentre;
                cmd.Parameters.Add("@vessel_softmar", System.Data.SqlDbType.NVarChar).Value = vessel;
                SqlDataReader rd = cmd.ExecuteReader();
                rd.Read();
                result = new CommissionManagersLegalEntity(rd);
                rd.Close();
                return result;
            }
            catch (Exception x)
            {
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.InterfaceSqlConnection.Close();
            }
        }
    }
}
